
let baseURL = "https://ppapi.kcclass.fun/pp-miniapp"
export {baseURL}
export function fail(message) {
    wx.showToast({
        title: message,
        mask: true,
        icon: "error"
    });
}

export function ok(message) {
    wx.showToast({
        title: message,
        mask: true,
        icon: "se"
    });
}

export function request({url, method, data, header}) {
    url = baseURL + url
    method =method || "GET"
    data = data || {}
    header = header || {'content-type': 'application/json'}
    if(wx.getStorageSync('loginUser')){
      header['token']= wx.getStorageSync('loginUser').token
    }
    return new Promise((resolve, reject) => {
        wx.request({
            url,
            method,
            data,
            header,
            success: (res) => {
                if (res.statusCode !== 200) {
                    fail("服务异常")
                    return
                }
                if(res.data.code===-2){
                  wx.navigateTo({
                    url: '/pages/login/index',
                  })
                  return;
                }
                if (res.data.code !== 200) {
                    fail(res.data.message)
                    return;
                }
                resolve(res.data)
            },
            fail: (error) => {
              console.log(error);
                fail("网络异常")
            }
        });
    });
}